package com.kkb.merchant.center.controller;


import com.github.pagehelper.PageInfo;
import com.kkb.merchant.center.api.BusinessInfoService;
import com.kkb.merchant.center.common.exception.DefinitionException;
import com.kkb.merchant.center.common.pojo.BusinessInfo;
import com.kkb.merchant.center.common.util.HttpResult;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/")
public class BusinessController {


    @Reference
    private BusinessInfoService businessInfoService;

    //商户列表
    @ResponseBody
    @GetMapping("api/getBusinessList")
    public HttpResult getBusinessList(@RequestParam(value="page",defaultValue="1")int pageNo, @RequestParam(value="pageSize",defaultValue="10")int pageSize) {
        PageInfo<BusinessInfo> businessList = businessInfoService.getBusinessList(pageNo+1,pageSize,-1);
        return HttpResult.success(businessList);
    }

    //商家审核
    @ResponseBody
    @GetMapping("api/businessReview")
    public HttpResult businessReview(@RequestParam(value="page",defaultValue="1")int pageNo, @RequestParam(value="pageSize",defaultValue="10")int pageSize) {
        PageInfo<BusinessInfo> businessList = businessInfoService.getBusinessList(pageNo+1,pageSize,0);
        return HttpResult.success(businessList);
    }

    @ResponseBody
    @PostMapping("auth/register")
    public HttpResult register(@RequestBody BusinessInfo businessInfo){
        //判断商家名称、店铺名称、用户名称不能为空
        if(StringUtils.isEmpty(businessInfo.getCompName())
                ||StringUtils.isEmpty(businessInfo.getShopInfo().getShopName())
                ||StringUtils.isEmpty(businessInfo.getUserInfo().getUserName()))
        {
            return HttpResult.defineError(new DefinitionException("请输入必填项"));
        }

        int compNameExist = businessInfoService.selectCompNameExist(businessInfo.getCompName());
        if(compNameExist>0){
            return HttpResult.defineError(new DefinitionException("该商家名称已经被占用"));

        }

        int shopNameIsExist = businessInfoService.shopNameIsExist(businessInfo.getShopInfo().getShopName());
        if(shopNameIsExist>0){
            return HttpResult.defineError(new DefinitionException("该店铺名称已经被占用"));

        }

        int userNameIsExist = businessInfoService.shopUserNameIsExist(businessInfo.getUserInfo().getUserName());
        if(userNameIsExist>0){
            return HttpResult.defineError(new DefinitionException("该用户名称已经被占用"));

        }

        int result = businessInfoService.register(businessInfo);
        return  HttpResult.success(result);
    }
}
